/*
 * Class PublicChat
 *
 * Version 1.0
 *
 * Sunday, August 17, 2008
 *
 * Created by Palidino76
 */

package palidino76.rs2.io.packets;

import palidino76.rs2.Server;
import palidino76.rs2.player.Player;
import palidino76.rs2.util.Misc;
import palidino76.rs2.Engine;

public class PublicChat implements Packet {
    /**
     * Handles player chatting.
     * @param p The Player which the frame should be handled for.
     * @param packetId The packet id this belongs to.
     * @param packetSize The amount of bytes being recieved for this packet.
     */
	     public void handlePacket(Player p, int packetId, int packetSize) {
        if (p == null || p.stream == null) {
            return;
        }
        int chatTextEffects = p.stream.readUnsignedWord();
        int numChars = p.stream.readUnsignedByte();
        String chatText = Misc.decryptPlayerChat(p.stream, numChars);
        if (chatText.startsWith("/")) {
            if (p.clanRoom.length() > 0) {
                Engine.clanChat.sendMessage(p, chatText.substring(1));
            }
			if (p.starter == 0) {
				p.frames.sendMessage(p, "You must type :: starter before speaking for the first time.");
				return;
			}
			if (p.muteType > 0) {
				p.frames.sendMessage(p, "You are are permanently muted for breaking a rule.");
				return;
			}
        } else {
           
                p.chatTextEffects = chatTextEffects;
                p.chatText = chatText;
                p.chatTextUpdateReq = true;
                p.updateReq = true;
        }
    }
}